<?php
/**
 * reptile dzscuz论坛发帖工具
 * @link      http://book.kfsimi.cn
 * @copyright Copyright (c) 2019-2020 apiframe.
 * @license   http://www.apiframe.cn
 * @package   apiframe/tools
 * @author    fengjian zhang mail:1224103366@qq.com
 * @version   1.0 Beta
 * @简介：此工具为DZ论坛数据库发帖工具！不经过dz论坛程序用apiframe框架直接写入数据库！
 
       $mbid     //帖子模板标题ID，注意是标题id不是文章！所以发帖均以此贴为模板！
	   
       $author   //发帖人
       $subject  //帖子标题
       $message  //帖子内容
	  
 */
namespace apiframe\tools;
class discuz {
	
		const shujuku = 'db';//数据库连接标记，具体看数据库配置文件！
		
	
		public function dzget($mbid,$author,$subject,$message){
	   	    $this->__zhuti($mbid,$author,$subject,$message); //写入帖子主题成功则返回主题内容数组
		}


	    public function __zhuti($mbid,$author,$subject,$message){ //DZ操作主题表  发帖第一步
			$this->db = db('pre_forum_thread',self::shujuku);
	    	$this->db->query('select * from pre_forum_thread where tid = ?',array($mbid));
	        $dwe = $this->db->queryFetchAll();
	        $dat = $dwe[0];
	        unset($dat['tid']);//删除现有数组字段，让tid自增加！
	    	$dat['dateline'] = time(); //发表时间
	    	$dat['lastpost'] = $dat['dateline']; //最后发表时间
	    	$dat['author'] = $author; //会员名
	    	$dat['subject'] = $subject; //文章标题 
	    	$dat['views'] = rand(28, 321); //浏览次数
	    	$dat['replies'] = rand(1, 33); //浏览次数
		    $this->db = db('pre_forum_thread',self::shujuku);
	        $tid = $this->db->add($dat);
	        $da = $this->__tiezi($mbid,$tid,$message,$dat); //写入帖子详情
		}
	
		
		public function __fenbiao(){ //DZ帖子分表取得pid
		    $this->db = db('pre_forum_post_tableid',self::shujuku);
	        $dat = $this->db->add();
	    	return $dat;
		}
		
		
	    public function __tiezi($mbid,$tid,$message,$data){ //DZ操作帖子表
			$this->db = db('pre_forum_post',self::shujuku);
	    	$this->db->query('select * from pre_forum_post where tid = ?',array($mbid));
	        $dwe = $this->db->queryFetchAll();
	        $dat = $dwe[0];
	        $pid = $this->__fenbiao(); //自增分表pid，返回pid
	        $dat['pid'] = $pid;//帖子分表id
	    	$dat['tid'] = $tid;//帖子主题id
	    	$dat['author'] = $data['author'];//帖子作者
	    	$dat['dateline'] = $data['dateline'];//发帖时间
	    	$dat['subject'] = $data['subject'];//帖子主题
	    	$dat['message'] = $message;//帖子内容
	    	$dat['port'] = rand(1000, 9999);//暂时不知道干嘛的，瞎定义个随机4位数！
		    $this->db = db('pre_forum_post',self::shujuku);
	        $datq = $this->db->add($dat);
	    	$this->__bankuai($dat['fid'],$dat['tid'],$dat['subject'],$dat['dateline'],$data['lastposter']);//调用版块信息更新
	    	$this->__yonghu($dat['authorid']);//调用用户信息表
		}
		
		
		public function __bankuai($fid,$tid,$subject,$dateline,$author){ //DZ更新论坛版块帖子数量
		    $this->db = db('pre_forum_forum',self::shujuku);
	        $this->db->where('fid = ?', array($fid))->field('threads', 1);//更新版块主题数量
	        $this->db->where('fid = ?', array($fid))->field('todayposts', 1);//今日发帖数量
	        $data = array('lastpost' => $tid.'	'.$subject.'	'.$dateline.'	'.$author);
	        $this->db->where('fid = ?', array($fid))->update($data);//g更新最后发帖
	        
		}
		
		public function __yonghu($authorid ){ //DZ更新用户信息（）
		    $this->db = db('pre_common_member_count',self::shujuku);
	        $this->db->where('uid = ?', array($authorid))->field('threads', 1);//更新用户发帖数量
		}
			
	

	
}